// 产成品入库公共类
try {
	Type.registerNamespace('FINPRODINUtil');
	FINPRODINUtil.updateScanNumForList = function(pkitem) {
			// 回写扫描数量listvo
			condition = {
				"pkitem": pkitem
			}
			NCMMPubUtil.queryVOByConditions(FINPRODINSCANVO, condition, function(scandata) {
				var scanednum = 0;
				if(scandata == null || scandata == "" | typeof(scandata) == 'undefined' || eval(scandata).length < 1) {
					scanednum = "";
				} else {
					var scanvos = eval(scandata);
					for(var i = 0; i < scanvos.length; i++) {
						scanednum = NCMMPubUtil.getSumScanedNum(scanednum, scanvos[i]["scansinglenum"]);
					}
				}
				NCMMPubUtil.queryVOByConditions(FINPRODINVO, condition, function(data) {
					if(data == null || data == "" | typeof(data) == 'undefined' || eval(data).length < 1)
						return;
					//$alert(data);
					var vos = eval(data);
					//$alert(vos);
					//$alert("更新产成品入库单数量");
					//$alert(vos[0]);
					//处理反斜杠特殊字符
					//vos[0] = NCMMPubUtil.htmlspecialjson2UI(vos[0]);
					//$alert(vos[0]);
					vos[0]["scannum"] = scanednum.toString();
					var attris = [];
					attris.push(FINPRODINVO.SCANNUM);
					NCMMPubUtil.updateVOWithAttris(FINPRODINVO, vos[0], attris);
				});
			});
		}
		// 提交的时候判断是否还存在相同来源的扫描记录
	FINPRODINUtil.checkOtherScanRecord = function(pkitem) {
		var flag = false;
		conditionForScan = {
			"pkitem": pkitem
		}
		NCMMPubUtil.queryVOByConditions(FINPRODINSCANVO, conditionForScan, function(data) {
			if(data != null && data != "") {
				data = eval(data);
				if(data.length > 1) {
					// $alert("当前记录存在其他来源相同的扫描记录，不能单独提交！");
					flag = true;
					return flag;
				}
			}
		});
		return flag;
	}

	// 由主数量和换算率计算数量
	FINPRODINUtil.getNastum = function(nnum, vchangerate) {
		if(nnum == null || nnum == "") {
			nnum = "0.00000";
		} else {
			nnum = parseFloat(nnum);
		}
		if(vchangerate == null || vchangerate == "") {
			vchangerate = "1.00000";
			var nastum = nnum / vchangerate;
		} else {
			var vchangearray = vchangerate.split("/");
			if(vchangearray == null || vchangearray.length != 2)
				return nnum;
			var up = parseFloat(vchangearray[0]);
			var down = parseFloat(vchangearray[1]);
			if(up == null || down == null)
				return nnum;
			var nastum = (nnum * down) / up;
		}
		return nastum;
	}

	// 由数量和换算率计算主数量
	FINPRODINUtil.getNum = function(nastnum, vchangerate) {
			if(nastnum == null || nastnum == "") {
				nastnum = "0.00000";
			} else {
				nastnum = parseFloat(nastnum);
			}
			if(vchangerate == null || vchangerate == "") {
				vchangerate = "1.00000";
				var num = nastnum * vchangerate;
			} else {
				var vchangearray = vchangerate.split("/");
				if(vchangearray == null || vchangearray.length != 2)
					return nastnum;
				var up = parseFloat(vchangearray[0]);
				var down = parseFloat(vchangearray[1]);
				if(up == null || down == null)
					return nastnum;
				var num = (nastnum * up) / down;
			}
			return num;
		}
		//处理精度
	FINPRODINUtil.processScale = function(jsonarray, numkeys, measurekey) {
		if(!NCMMPubUtil.isTableExist(MeasdocVO) || jsonarray == null || jsonarray == "" || jsonarray == "[]") {
			return;
		}
		for(var i = 0; i < jsonarray.length; i++) {
			var json = jsonarray[i];
			var wheresql = "where pk_measdoc='" + json[measurekey] + "'"
			NCMMPubUtil.queryVO(MeasdocVO, wheresql, function(data) {
				if(data != null && data != "" && data != "[]") {
					var meajson = $stringToJSON(data);
					var scale = meajson[0].bitnumber;
					for(var j = 0; j < numkeys.length; j++) {
						var numStr = json[numkeys[j]];
						if(numStr != null && numStr != "") {
							var num = parseFloat(numStr);
							json[numkeys[j]] = num.toFixed(scale);
						}
					}
				}
			});
		}
	}
	FINPRODINUtil.processGroupScale = function(jsonarray, numkeys, measurekey) {
			if(!NCMMPubUtil.isTableExist(MeasdocVO) || jsonarray == null || jsonarray == "" || jsonarray == "[]") {
				return;
			}
			for(var i = 0; i < jsonarray.length; i++) {
				var json = jsonarray[i];
				var rowjsons = json["row"];
				if(rowjsons == null || rowjsons == "" || rowjsons == "[]") {
					continue;
				}
				var wheresql = "where pk_measdoc='" + rowjsons[0][measurekey] + "'"
				NCMMPubUtil.queryVO(MeasdocVO, wheresql, function(data) {
					if(data != null && data != "" && data != "[]") {
						var meajson = $stringToJSON(data);
						var scale = meajson[0].bitnumber;
						var sumNumStr = json["scansumnum"];
						if(sumNumStr != null && sumNumStr != "") {
							var sumNum = parseFloat(sumNumStr);
							json["scansumnum"] = sumNum.toFixed(scale);
						}
						for(var k = 0; k < rowjsons.length; k++) {
							for(var j = 0; j < numkeys.length; j++) {
								var numStr = rowjsons[k][numkeys[j]];
								if(numStr != null && numStr != "") {
									var num = parseFloat(numStr);
									rowjsons[k][numkeys[j]] = num.toFixed(scale);
								}
							}
						}
					}
				});
			}
		}
		//设置数字精度
	FINPRODINUtil.getScaleNum = function(numStr, pk_measure) {
			if(!NCMMPubUtil.isTableExist(MeasdocVO)) {
				return numStr;
			}
			var num;
			var wheresql = "where pk_measdoc='" + pk_measure + "'"
			NCMMPubUtil.queryVO(MeasdocVO, wheresql, function(data) {
				num = parseFloat(numStr);
				if(data != null && data != "" && data != "[]") {
					var meajson = $stringToJSON(data);
					var scale = meajson[0].bitnumber;
					num = num.toFixed(scale);
				}
			});
			return num;
		}
		//获取单位的精度
	FINPRODINUtil.getScaleDigit = function(pk_measure) {
			if(!NCMMPubUtil.isTableExist(MeasdocVO)) {
				return null;
			}
			var scale = "";
			var wheresql = "where pk_measdoc='" + pk_measure + "'"
			NCMMPubUtil.queryVO(MeasdocVO, wheresql, function(data) {
				if(data != null && data != "" && data != "[]") {
					var meajson = $stringToJSON(data);
					scale = meajson[0].bitnumber;
				}
			});
			return scale;
		}
		//校验输入数字的精度是否正确
	FINPRODINUtil.validateNumScale = function(numStr, pk_measure) {
			var returnflag;
			var scale = FINPRODINUtil.getScaleDigit(pk_measure);
			if(scale == null || scale == "") {
				returnflag = true;
				return returnflag;
			}
			var strs = numStr.split(".");
			if(strs.length == 1) {
				returnflag = true;
			} else if(strs.length == 2 && strs[1].length <= scale) {
				returnflag = true;
			} else {
				returnflag = false;
			}
			return returnflag;
		}
		//判断输入的字符串是否是数字，包括正、负、0
	FINPRODINUtil.isNumber = function(inputstr) {
		var zregex = /^\d+(\.\d*)?$/;
		var fregex = /^((-\d*(\.\d*)?)|(0+(\.0*)?))$/;
		if(inputstr == null || inputstr == "") {
			return false;
		}
		if(!zregex.test(inputstr) && !fregex.test(inputstr)) {
			return false;
		}
		return true;
	}
	FINPRODINUtil.processBillDataFromNC = function(VOClass, appbillname, condition, callback) {
		$js.showLoadingBar({
			msg: "正在连接服务器..."
		});
		var host = $cache.read('host');
		var port = $cache.read('port');
		$app.checkNetWorkReachable(host, port, function(param, args) {
			if(args.is_reachable == 'true') {
				$app.showLoadingBar({
					msg: "正在下载……"
				});
				NCMMDBUtil.loadNCBaseData(FINPRODINVO, FINPRODINVO, NCMMHttpUtil.BILL, condition, function(data) {
					//如果没有从NC端下载到数据，则本地记录不更新
					if(data == null) {
						$js.hideLoadingBar();
						$toast("没有获取到符合条件的订单");
						return;
					}
					if(data != null && typeof(data) == 'string') {
						$js.hideLoadingBar();
						$toast("下载失败：" + data);
						return;
					}
					//										$alert("process data=" + $jsonToString(data));
					// 如果没有信息直接返回
					var billinfos = data.billinfos || {};
					var props = Object.getOwnPropertyNames(billinfos) || [];
					if(props.length == 0) {
						if($isJSONObject(data)) {
							$js.hideLoadingBar();
							$toast("没有下载到【" + appbillname + "】");
						} else {
							$js.hideLoadingBar();
							$toast(data);
						}
						return;
					}
					//下载数据的条数
					//var vocount = NCMMDBUtil.getBillCount(data);
					// 把NC数据拉平
					var billinfos_local = [];
					billinfos_local = FINPRODINUtil.processScanNumForList(data);
					// 清空本地表记录
					NCMMDBUtil.clearAllOldData(VOClass, null);
					//billinfos_local = NCMMMOUtil.parseNCData2Local(billinfos);
					//					$alert("billinfos_local=" + $jsonToString(billinfos_local));
					// TODO 执行拓展服务insert操作
					var attrs = VOClass.prototype.getAllAttrs();
					var fields = attrs.map(function(row, i, arr) {
						return row.name;
					});

					var valueMapList = Array.prototype.map.call(billinfos_local, function(row, i, arr) {
						var billinfo_local = $stringToJSON((row || {}));
						var values = attrs.map(function(attr, i, array) {
							var fname = attr.name;
							if(!!billinfo_local[fname]) {
								if(billinfo_local[fname] != "" && typeof(billinfo_local[fname]) == "string") {
									billinfo_local[fname] = NCMMPubUtil.htmlspecialchars2sqliteex(billinfo_local[fname]);
								}
								return billinfo_local[fname];
							} else {
								if(attr.type == NCMMPubUtil.VALUE_TYPE_INTEGER) {
									return 0;
								} else if(attr.type == NCMMPubUtil.VALUE_TYPE_REAL) {
									return 0.0;
								} else {
									return "";
								}
							}
						});
						return values;
					});

					var sum = valueMapList.length;
					//					$alert("valueMapList=" + $jsonToString(valueMapList));

					$app.replaceBatch({
						db: NCMMPubUtil.DatabaseName,
						table: VOClass.prototype.getTableName(),
						fields: fields,
						values: valueMapList,
						callback: function(args_null, args) {
							if(args.code == '1000' || args.code == 1000) {
								$js.hideLoadingBar();
								$toast("【" + appbillname + "】下载了【" + sum + "】条数据。");
								if(callback) {
									callback(sum);
								}
							} else {
								$js.hideLoadingBar();
								$js.toast("【" + appbillname + "】下载异常");
								if(callback) {
									callback(null);
								}
							}
						}
					});

				});

			} else {
				$js.hideLoadingBar();
				$js.toast("下载失败请检查网络设置！");
			}
		});

	};

	// 处理要插入的数据
	FINPRODINUtil.processScanNumForList = function(vos) {
			var billdatas = [];
			var json = {};
			var existpkitems = [];
			var scannumdatas = [];
			// 更新数据前在本地数据库查找扫描数量大于0的
			wheresql = "where scannum > 0";
			NCMMPubUtil.queryVO(FINPRODINVO, wheresql, function(datas) {
				if(datas != null) {
					datas = eval(datas);
					scannumdatas = datas;
					if(datas.length > 0) {
						for(var index = 0; index < datas.length; index++) {
							// 放到一个json数组中
							json[(datas[index])["pkitem"]] = datas[index];
						}
					}
				}
			});
			var billinfos = vos.billinfos;
			for(var i = 0; i < billinfos.length; i++) {
				// 有多少个子项插入多少个vo
				var items = billinfos[i].billinfo.items;
				for(var j = 0; j < items.length; j++) {
					var billdata = $.extend({}, items[j].item, billinfos[i].billinfo.head);
					var pkitem = billdata["pkitem"];
					if(NCMMPubUtil.isNotEmpty(json[pkitem])) {
						existpkitems.push(pkitem);
						// 如果要下载的数据包含了本地有扫描记录的数据则更新扫描数量后再插入
						billdata["scannum"] = json[pkitem]["scannum"];
					}
					billdatas.push(billdata);
				}
			}
			for(var i = 0; i < scannumdatas.length; i++) {
				if(existpkitems.indexOf(scannumdatas[i]["pkitem"]) == -1) {
					billdatas.push(scannumdatas[i]);
				}
			}
			return billdatas;
		}
		//处理下单引号
	FINPRODINUtil.htmlsqmchars2UI = function(s) {
		if(s == null || s == NaN)
			return s;

		if(typeof(s) == "string" && s != "") {
			s = s.replace(/[']?/mg, function(tokens, index, currentStr) {
				if(tokens.charCodeAt(0) == 39) {
					return tokens + tokens;
				}
				return tokens;
			});
		}

		return s;
	};

} catch(e) {
	$e(e);
}